<?php
echo "Hello, World!\n";
$a_bool = TRUE;
$a_str = "foo";
$a_str2 = 'foo';
$an_int = 12;

echo gettype($a_bool);
echo("\n");
echo gettype($a_str);
echo("\n");
if (is_int($an_int)){
	$an_int += 4;
}
echo($an_int);
echo("\n");
if(is_bool($a_bool)){
	echo "Bool: $a_bool";
}
echo("\n");

$juice = "apple";
echo "He drank some $juice juice.".PHP_EOL;
echo "He drank some juice made of $juice".PHP_EOL;

$str = 'This is a test.';
$first = $str[0];
echo($first);
echo("\n");

$third = $str[5];// 获取第6个字符
echo ($third);
echo("\n");

// it use the way of length -1, and we want to get the length of str, we can use "strlen"
$last_one = $str[strlen($str) -1];
// get the last one
echo($last_one);
echo("\n");

// or you can put -1 ,it can also get the value "."
$last_one2 = $str[-1];
echo($last_one2);
echo ("\n");

class A{
	public static function who(){
		echo "A\n";
	}
}

class B extends A{
	public static function who(){
		echo "B\n";
	}
}

class C extends B{
	public static function who()
	{
		echo "C\n";
	}
}

call_user_func(array('B', 'parent::who'));

$double = function($a) {
	return $a * 2;
};

$numbers = range(1, 5);

$new_numbers = array_map($double, $numbers);

print implode(' ', $new_numbers);
echo "\n";

// 1. echo
echo "Hello, World!\n";

// 2. print
print "Hello, World!\n";

// 3. printf
$name = "John";
$age = 25;
printf("Name: %s, Age: %d\n", $name, $age);

// 4. print_r
$array = array("foo", "bar", "baz");
print_r($array);
echo "\n";

// 5. var_dump
$var = 3.14;
var_dump($var);

// 6. var_export
$var = array("foo", "bar");
var_export($var);
echo "\n";

$a = 1;
$b = 2;

function Sum(){
    $a = 3;
    $b = 4;
	$b = $b + $a;
}
# 在php中，外部定义的变量和内部定义的变量之间不可以访问，就是函数内部需要调用全局变量时，不能调用，而在函数内加入global时，就可以使用全局变量了。
Sum();
echo $b;
echo "\n";
echo "\n";
echo "test1的输出";
echo "\n";
function test1()
{
	static $count = 0;
	echo $count;
	echo "\n";
	$count++;
	if ($count < 10){
		test1();
	}
	$count--;
}
# 虽然在函数中count是static类型的，但是在函数外面还是访问不到的。
# echo $count
test1();

# 常量
define("f00", "something");
echo f00;
echo "\n";

class Foo{
	static $variable = 'static property';
	static function Variable(){
		echo 'Method Variable called';
	}
}

echo Foo::$variable;
echo "\n";
$variable = "Variable";
echo $variable;
echo "\n";
Foo::$variable();
echo "\n";


class Base {
    public function sayHello() {
        echo 'Hello ';
    }
}
# 这里是个trait，
trait SayWorld {
    public function sayHello() {
        parent::sayHello();
        echo 'World!';
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld();
$o->sayHello();
echo "\n";
?>
